package ext.tianma.doc.util;

import java.io.File;
import java.rmi.RemoteException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;

import ext.com.iba.IBAUtil;
import ext.generic.erp.common.CommonPDMUtil;
import ext.tianma.util.PropertiesUtil;
import wt.doc.WTDocument;
import wt.fc.WTObject;
import wt.mail.EMailMessage;
import wt.util.WTException;
/**
 * 邮件通知DCC
 * @author deng
 *
 */
public class EmailNoticeDCC {
	private static String configFilePath = "codebase" + File.separator + "ext"
			+ File.separator + "tianma" + File.separator + "cfg"
			+ File.separator + "TianmaPro.properties";
	private static Locale LOCALE = Locale.CHINA;
	public static final String  Item  = "Item";//物料编号
	public static final String  productType  = "productType";//产品型号
	//public static final String  versions  = "versionInfo.identifier.versionId";//版号
	//public static final String  folder  = "folder.id";//文件名称
	public static final String  modulesDevelopedLand  = "modulesDevelopedLand";//研发地
	public static final String  moduleOrigin  = "moduleOrigin";//生成地
	
	public static void sendEmail(WTObject pbo) throws WTException {
		
		PropertiesUtil ut = new PropertiesUtil(configFilePath);
		String emails = ut.getValueByKey("DCC.Email");
		System.out.println("--------emails---------"+emails);
		List<String> list = new ArrayList<String>();
		if(!"".equals(emails) && emails != null){
	        String[] email = emails.split(";");//邮箱错误处理
	        for (int i = 0; i < email.length;i++){
	        	list.add(email[i]);
	        }
		}
		for (int ei = 0; ei < list.size(); ei++) {
			String emailAddr = list.get(ei);
			System.out.println("-----------emailAddr---------"+emailAddr);
			emailAddr += ";";
			// 邮件内容
			StringBuffer body = new StringBuffer();
			String content = emailContent(pbo);
			System.out.println("----------content---------"+content);
			body.append(content);
			String[] emailSendTo = emailAddr.split(";");//邮箱拼接
			EMailMessage email = EMailMessage.newEMailMessage();
			email.addEmailAddress(emailSendTo);
			// 邮件主题
			StringBuffer title = new StringBuffer();
			WTDocument doc = getDocByPBO(pbo);
			String name = doc.getName();
			System.out.println("--------name-------"+name);
			title.append(name+"文档已发布成功");
			email.setSubject(title.toString());
			email.setMultipartSubtype("text/html;charset=\"UTF8\"");
			email.addPart(body.toString(), "text/html;charset=\"UTF8\"");
			email.send(false);
		}
	}
	/**
	 * 邮件主内容
	 * 
	 * @param pbo
	 * @return
	 * @throws WTException
	 */
	public static String emailContent(WTObject pbo)throws WTException {
		
		String content = "";
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy/MM/dd");//设置日期格式
		/*TimeZone tz = WTContext.getContext().getTimeZone();
		TimeZone.setDefault(tz);*/
		String currentDate = simpleDateFormat.format(new Date());//生效日期
		WTDocument doc = getDocByPBO(pbo);//根据pbo获得文档
		try {
			String Item1 = IBAUtil.getIBAValue(doc,Item)==null ? "": ((String) IBAUtil.getIBAValue(doc,Item));//料号
			String productType1 = IBAUtil.getIBAValue(doc,productType)==null ? "": ((String) IBAUtil.getIBAValue(doc,productType));//型号
			//String versions1 = ((String) IBAUtil.getIBAValue(doc,versions));//版号
			String version = CommonPDMUtil.getVersion(doc);//版号
			String folderPath = doc.getFolderPath();
			String[] folders = folderPath.split("/");
			String folder = folders[folders.length-2];//文件名称
			//String folder1 = ((String) IBAUtil.getIBAValue(doc,folder));//文件名称
			String updateBy = CommonPDMUtil.getModifierName(doc);//拟制人
			String modulesDevelopedLand1 = IBAUtil.getIBAValue(doc,modulesDevelopedLand)==null ? "": ((String) IBAUtil.getIBAValue(doc,modulesDevelopedLand));//研发地
			String moduleOrigin1 = IBAUtil.getIBAValue(doc,moduleOrigin)==null ? "": ((String) IBAUtil.getIBAValue(doc,moduleOrigin));//生产地
			String name = doc.getName();//文档名称
			String productState = doc.getState().getState().getDisplay(LOCALE);//状态
			System.out.println("----------Item1----------"+Item1);
			System.out.println("----------productType1----------"+productType1);
			System.out.println("----------version----------"+version);
			System.out.println("----------folder----------"+folder);
			System.out.println("----------updateBy----------"+updateBy);
			System.out.println("----------modulesDevelopedLand1----------"+modulesDevelopedLand1);
			System.out.println("----------moduleOrigin1----------"+moduleOrigin1);
			System.out.println("----------name----------"+name);
			System.out.println("----------productState----------"+productState);
			if(-1!=name.indexOf("临时图纸")) {
				String type = "是";//是临时图纸
				content = email(currentDate, content, Item1, productType1, version, folder, updateBy,
						modulesDevelopedLand1, moduleOrigin1, productState, type);
			}else {
				String type = "不是";//不是临时图纸
				content = email(currentDate, content, Item1, productType1, version, folder, updateBy,
						modulesDevelopedLand1, moduleOrigin1, productState, type);
			}
		} catch (RemoteException e) {
			e.printStackTrace();
		}
		return content;
	}
	/**
	 * 邮件主内容
	 * 
	 * @param currentDate
	 * @param content
	 * @param Item1
	 * @param productType1
	 * @param version
	 * @param folder
	 * @param updateBy
	 * @param modulesDevelopedLand1
	 * @param moduleOrigin1
	 * @param productState
	 * @param type
	 * @return
	 */
	private static String email(String currentDate, String content, String Item1, String productType1, String version,
			String folder, String updateBy, String modulesDevelopedLand1, String moduleOrigin1, String productState,
			String type) {
		content += "<table border=\"1\" align=\"center\">";
		content += "<tr align=\"center\"><th>料号</th><th>型号</th><th>版号</th><th>文件名称</th><th>生效日期</th><th>拟制人</th><th>研发地</th><th>生产地</th><th>临时图纸</th><th>状态</th></tr>";
		content += "<tr align=\"center\"><td>"
				+ Item1
				+ "</td><td>"
				+ productType1
				+ "</td><td>"
				+ version
				+ "</td><td>"
				+ folder
				+ "</td><td>"
				+ currentDate
				+ "</td><td>"
				+ updateBy
				+ "</td><td>"
				+ modulesDevelopedLand1
				+ "</td><td>"
				+ moduleOrigin1
				+ "</td><td>"
				+ type
				+ "</td><td>"
				+ productState
				+ "</td></tr>";
		content += "</table>";
		return content;
	}
	/**
	 * 根据pbo获得文档
	 * 
	 * @param pbo
	 * @return
	 */
	public static WTDocument getDocByPBO(WTObject pbo) {
		wt.fc.WTObject obj = (wt.fc.WTObject) pbo;
		if (obj instanceof WTDocument) {
			WTDocument doc = (WTDocument)obj;
			return doc;
		}
		return null;
	}
}
